package com.wyyc.common.carillegalrecord.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wyyc.common.car.repository.CarRepository;
import com.wyyc.common.carillegalrecord.repository.CarIllegalRecordRepository;
import com.wyyc.common.carillegalrecord.service.CarIllegalRecordService;
import com.wyyc.common.city.repository.CityRepository;
import com.wyyc.entity.*;
import com.wyyc.ext.ExtInterface;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Hwei~ on 2017/6/7.
 */
@Service
public class CarIllegalRecordServiceImpl implements CarIllegalRecordService{

    @Resource
    private CarIllegalRecordRepository carIllegalRecordRepository;
    @Resource
    private CarRepository carRepository;
    @Resource
    private CityRepository cityRepository;

    @Override
    public ResJson getNewIllegal(int carId){
        ResJson resJson = new ResJson();
        try{
            String engineno = "";
            String classno = "";
            //计数器
            int index = 0;
            //查询车辆
            MyCarEntity myCarEntity = carRepository.findOne(carId);
            //查询城市代码
            CitysEntity citysEntity = cityRepository.findByCityCode(myCarEntity.getCityCode());
            //判断城市是否需要发动机号和车架号
            if (citysEntity.getEngine().equals("1")){
                //需要并拿到车架号
                String myCarEngineno = myCarEntity.getEngineno();
                //判断是否需要车架号的后几位
                if (!citysEntity.getEngineno().equals("0")){
                    //需要并拿到该城市车架号的几位
                    int cityEngineno = Integer.parseInt(citysEntity.getEngineno());
                    //截取字符
                    engineno=myCarEngineno.substring(myCarEngineno.length() - cityEngineno, myCarEngineno.length());
                }else{
                    //不需要
                    engineno = myCarEntity.getEngineno();
                }
            }
            if (citysEntity.getClazz().equals("1")){
                String myCarClassno = myCarEntity.getClassno();
                if (!citysEntity.getClassno().equals("0")){
                    int cityClassno = Integer.parseInt(citysEntity.getClassno());
                    classno = myCarClassno.substring(myCarClassno.length()-cityClassno,myCarClassno.length());
                }else{
                    classno = myCarEntity.getClassno();
                }
            }
            //String result = ExtInterface.getCarIllegal(myCarEntity.getCityCode(),myCarEntity.getHphm(),engineno,classno);
            //调试代码
            String result = "{\"province\":\"HB\",\"city\":\"HB_HD\",\"hphm\":\"冀DHL327\",\"hpzl\":\"02\",\"lists\":[{\"date\":\"2013-12-30 11:57:29\",\"area\":\"316省道53KM+200M\",\"act\":\"16362:驾驶中型以上载客载货汽车、校车、危险物品运输车辆以外的其他机动车在高速公路以外的道路上行驶超过规定时速20%以上未达50%的\",\"code\":\"\",\"fen\":\"6\",\"money\":\"100\",\"handled\":\"0\"}]}";
            JSONObject jsonObject = JSON.parseObject(result);
            String recordJson = jsonObject.getString("lists");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<CarIllegalRecordJson> carIllegalRecordJsonList = JSON.parseArray(recordJson, CarIllegalRecordJson.class);
            if (carIllegalRecordJsonList.size()!=0){
                //有数据
                for (CarIllegalRecordJson c:carIllegalRecordJsonList){
                    //获取本地数据
                    List<CarIllegalRecordEntity> carIllegalRecordEntityList = carIllegalRecordRepository.findByCarId(myCarEntity.getId());
                    Date date = sdf.parse(c.getDate());
                    //本地数据是否为空
                    if (carIllegalRecordEntityList.size()!=0) {
                        int x = 0;
                        for (CarIllegalRecordEntity ce : carIllegalRecordEntityList) {
                            System.out.println(ce.getFineTime().getTime());
                            System.out.println(date.getTime());
                            if (ce.getFineTime().getTime()==date.getTime()) {
                                x++;
                            }
                        }
                        if (x == 0) {
                            CarIllegalRecordEntity cr = new CarIllegalRecordEntity();
                            cr.setUserId(myCarEntity.getUserId());
                            cr.setCarId(myCarEntity.getId());
                            cr.setMoney(c.getMoney());
                            cr.setFen(c.getFen());
                            cr.setAddate(new java.sql.Date(System.currentTimeMillis()));
                            cr.setCreateTime(new Timestamp(System.currentTimeMillis()));
                            cr.setFineDate(new java.sql.Date(date.getTime()));
                            cr.setFineTime(new Timestamp(date.getTime()));
                            cr.setArea(c.getArea());
                            cr.setCode(c.getCode());
                            cr.setAct(c.getAct());
                            cr.setHandled(c.getHandled());
                            myCarEntity.setMoney(myCarEntity.getMoney()+Integer.parseInt(c.getMoney()));
                            myCarEntity.setFen(myCarEntity.getFen()+Integer.parseInt(c.getFen()));
                            myCarEntity.setIllegal(myCarEntity.getIllegal()+1);
                            carIllegalRecordRepository.save(cr);
                            carRepository.save(myCarEntity);
                            index++;
                        }
                    }else{
                        //插入数据
                        CarIllegalRecordEntity cr = new CarIllegalRecordEntity();
                        cr.setUserId(myCarEntity.getUserId());
                        cr.setCarId(myCarEntity.getId());
                        cr.setMoney(c.getMoney());
                        cr.setFen(c.getFen());
                        cr.setAddate(new java.sql.Date(System.currentTimeMillis()));
                        cr.setCreateTime(new Timestamp(System.currentTimeMillis()));
                        cr.setFineDate(new java.sql.Date(date.getTime()));
                        cr.setFineTime(new Timestamp(date.getTime()));
                        cr.setArea(c.getArea());
                        cr.setCode(c.getCode());
                        cr.setAct(c.getAct());
                        cr.setHandled(c.getHandled());
                        myCarEntity.setMoney(myCarEntity.getMoney()+Integer.parseInt(c.getMoney()));
                        myCarEntity.setFen(myCarEntity.getFen()+Integer.parseInt(c.getFen()));
                        myCarEntity.setIllegal(myCarEntity.getIllegal()+1);
                        carIllegalRecordRepository.save(cr);
                        carRepository.save(myCarEntity);
                        index++;
                    }
                }

            }
            resJson.setStatus(0);
            resJson.setDesc("违章信息更新完成,共"+index+"条!");
            return resJson;
        }catch (Exception e){
            e.printStackTrace();
            resJson.setStatus(1);
            resJson.setDesc("违章信息更新失败!");
            return resJson;
        }
    }

    @Override
    public ResJson getIllegal(int carId) {
        ResJson resJson = new ResJson();
        List<CarIllegalRecordEntity> carIllegalRecordEntityList = carIllegalRecordRepository.findByCarId(carId);
        resJson.setStatus(0);
        resJson.setDesc("用户违章信息查询成功!");
        resJson.setResult(JSON.toJSONString(carIllegalRecordEntityList));
        return resJson;
    }
}
